package com.taobao.action;

import java.util.ArrayList;
import java.util.List;

import com.taobao.pojo.Line;
import com.taobao.pojo.Smaller;
import com.taobao.pojo.Title;
import com.taobao.pojo.UserInfo;
import com.taobao.server.ConfigServer;
import com.taobao.ui.widget.SmallerWidget;

public class SmallerPriorityAction extends CommonAction {

    private ConfigServer configServer;

    @Override
    public void doAction(Object uiObject) {
        Integer buttonNo = (Integer) getData("source");
        SmallerWidget smallerWidget = (SmallerWidget) uiObject;
        UserInfo userInfo = smallerWidget.getPojo(UserInfo.class);
        
        List<Smaller> selected = smallerWidget.getSelectSmaller();
        List<Smaller> all = userInfo.getSmallers();
        for (Smaller smaller : selected) {
            for (Smaller smallerBak : all) {
                if (smallerBak.getSmallerName().equals(smaller.getSmallerName())) {
                    smallerBak.setPriority(buttonNo.intValue());
                    break;
                }
            }
        }
        
        List<Line> lines = new ArrayList<Line>();
        for (Smaller sm : all) {
            lines.add(new Line(new Title(ConfigServer.SMALLERS), sm.getSmallerName(), sm.getPriority() + ", " + sm.getTbPwd() + ", " + sm.getTid()));
        }
        configServer.updateAll(ConfigServer.SMALLERS, lines);
        smallerWidget.fillWidget(userInfo);
    }

}
